import pygame
import random

black = (0, 0 ,0)
white = (255,255,255)
red = (255,0,0)
blue = (0,0,255)
green = (0,255,0)
framesPerSecond = 30
screen_width = 700
screen_height = 400
playerSpeed = 1
 
class Block(pygame.sprite.Sprite):
    def __init__(self,color = black):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([20,15])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.dx = 0
        self.dy = 0
        #will update every frame so we should control the value of dx
    def update(self):
        self.rect.move_ip(self.dx, self.dy)
        
class Player(Block):
    def __init__(self):
        Block.__init__(self,blue)
    
    def update(self):
        Block.update(self)
        if self.rect.x < 0:
            self.rect.x = 0
        elif self.rect.x > screen_width - self.rect.width:
            self.rect.x = screen_width - self.rect.width
        if self.rect.y < 0:
            self.rect.y = 0
        elif self.rect.y > screen_height - self.rect.height:
            self.rect.y = screen_height - self.rect.height
        
class PointBlock(Block):
    def __init__(self, dx, dy):
        Block.__init__(self)
        self.dx = dx
        self.dy = dy
        
    def update(self):
        Block.update(self)
        
pygame.init()

screen = pygame.display.set_mode([screen_width,screen_height])

block_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()
for i in range(50):
    block = PointBlock(0,0)
    block.rect.x = random.randrange(screen_width)
    block.rect.y = random.randrange(screen_height)
    
    block_list.add(block)
    all_sprites_list.add(block)
    
player = Player()
all_sprites_list.add(player)

clock = pygame.time.Clock()
score = 0

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT : 
            pygame.quit()
            exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                player.dy -= playerSpeed
            if event.key == pygame.K_DOWN:
                player.dy += playerSpeed
            if event.key == pygame.K_LEFT:
                player.dx -= playerSpeed
            if event.key == pygame.K_RIGHT:
                player.dx += playerSpeed
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                player.dy += playerSpeed
            if event.key == pygame.K_DOWN:
                player.dy -= playerSpeed
            if event.key == pygame.K_LEFT:
                player.dx += playerSpeed
            if event.key == pygame.K_RIGHT:
                player.dx -= playerSpeed   
      
    screen.fill(white)     
    all_sprites_list.update()
    all_sprites_list.draw(screen)
    pygame.display.flip()
    block_hit_list = pygame.sprite.spritecollide(player, block_list, True)
    
    if len(block_hit_list) > 0 :
        score = score + len(block_hit_list)
    print score
        